<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8" />
    <title>搜索任务详情</title>
	<link rel="stylesheet" type="text/css" href="/static/basic/plugins/easyui/themes/bootstrap/easyui.css">
	<link href="/static/basic/plugins/fontawesome-free-5.15.4-web/css/all.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="/static/m.css?v=new2" >
	<link rel="stylesheet" type="text/css" href="/static/basic/plugins/webuploader/webuploader.css?v=new2">
</head>
<body>
<div id="window-mmask" class="window-mmask" ><div class="window-mmask-msg" >页面加载中,请稍候...</div></div>
<div id="layout1" class="easyui-layout" data-options="fit:true"  >
	<div data-options="region:'center',title:'主表'" >
		<table id="gridList" ></table>
	</div>
	<div data-options="region:'south',collapsible:false,split:true,title:'子查询表'" style="height:50%;">
		<div class="easyui-layout" data-options="fit:true" style="">
			<table id="grid2" ></table>
		</div>
	</div>
</div>
<div id="searchbar">
<div class="maction">
		<a href="javascript:void(0)" id="importBtn1" class="easyui-linkbutton webuploader-container" data-options="plain:true,iconCls:'mbutton'">导入excel-覆盖</a>
	   <a href="javascript:void(0)" id="importBtn2" class="easyui-linkbutton webuploader-container" data-options="plain:true,iconCls:'mbutton'">导入excel-追加</a>
	<a href="javascript:void(0)" id="importBtn3" class="easyui-linkbutton " data-options="plain:true,iconCls:'mbutton'">初始化子表数据</a>
	<a href="javascript:void(0)" id="initBtn4" class="easyui-linkbutton " data-options="plain:true,iconCls:'mbutton'">初始化查询</a>
		<a href="javascript:void(0)" id="btn3" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-trash-alt mbutton color-c5'">删除</a>
		<a href="javascript:void(0)" id="saveButton" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-save mbutton'">保存</a>
	<a href="javascript:void(0)" id="exportData1" class="easyui-linkbutton" data-options="plain:true,iconCls:'mbutton'">导出数据</a>
</div>
<form id="searchForm" >
	Id<input id="search-Id" name="Id" class="easyui-textbox" >
	b<input id="search-b" name="b" class="easyui-textbox" >
	<a id="searchButton" href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-search mbutton'" >查询</a>
</form>
</div>
<div id="searchbar2">
<div class="maction">

	<a href="javascript:void(0)" id="importBtn4" class="easyui-linkbutton webuploader-container" data-options="plain:true,iconCls:'mbutton'">导入数据-追加</a>
		<a href="javascript:void(0)" id="btn6" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-trash-alt mbutton color-c5'">删除</a>
		<a href="javascript:void(0)" id="saveButton2" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-save mbutton'">保存</a>
	<a href="javascript:void(0)" id="exportData2" class="easyui-linkbutton" data-options="plain:true,iconCls:'mbutton'">导出数据</a>
	<a href="javascript:void(0)" id="insertToData1" class="easyui-linkbutton" data-options="plain:true,iconCls:'mbutton'">新增数据到表1</a>
</div>
	<form id="searchForm2" >
		Id<input id="search2-Id" name="Id" class="easyui-textbox" >
		a<input id="search2-a" name="a" class="easyui-textbox" >
		<a id="searchButton2" href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-search mbutton'" >查询</a>
	</form>
</div>
<script src="/static/basic/plugins/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="/static/basic/plugins/easyui/jquery.easyui.min.js"></script>
<script src="/static/basic/plugins/easyui/locale/easyui-lang-zh_CN.js"></script>
<script src="/static/extensions/jquery.jdirk.js"></script>
<script src="/static/extensions/jeasyui.extensions.base.loading.js"></script>
<script src="/static/init.js"></script>
<script src="/static/searchEdit.js?v=12"></script>
<script src="/static/enum.js"></script>
<script type="text/javascript" src="/static/basic/plugins/webuploader/webuploader.html5only.min.js"></script>
<!-- <script src="/static/basic/plugins/moment.min.js"></script> -->
<script>
var taskId=$.query.get("taskId");
var editRow=null;
var report1=null;
var report2=null;
var startTimeout=0;
var mainId;//主表id
//document ready
$(function(){
	//$("#layout1").layout("panel","center").panel("setTitle","setTitle");
	//1.先查询任务信息
	$.ajax({
		type: "post",
		contentType:"application/json",
		async:false,
		url:"/simple/namedList/select_excel_report_name_by_taskId",
		data:JSON.stringify({taskId:taskId}),
		success:function(result){
			if(result.success){
				console.log(result.data);
				if(result.data[0].tableName=='excel_data1'){
					report1=result.data[0];
					$("#layout1").layout("panel","center").panel("setTitle",report1.shortName);
				}

			}else{
				$.messager.alert("提示", "查询配置信息失败！<br>"+result.message);
			}
		}
	})
	grid1=new uniontrip.datagrid.searchEdit({
		idField:'Id',
		page_id:"491328710977257472",
		grid_id:"gridList",
		sqlname:"grid_491328710977257472_excel_data1",
		showCheckbox:false,
		rownumbers:false,
		toolbar_id:"searchbar",
		//initColumnUrl:"/",
		gridParam:{"taskId":taskId,"ascOrDesc":"asc","orderBy":"Id"},
		initColumnParam:{"taskId":taskId},
		onClickRow:function(rowIndex, rowData){
			if(rowData.Id!=mainId){
				mainId=rowData.Id;
				grid2.loadParam({mapId:mainId});
			}
		},
		column_formatter:{
			content:function(value,row,index){
				//return value;
				return "<pre style='overflow:hidden'>"+value+"</pre>";
			},
			example:function(value,row,index){
				//return value;
				return "<pre style='overflow:hidden'>"+value+"</pre>";
			},
		},
		searchParamOperator:{
			title:"contain"
		},
	});
	grid2=new uniontrip.datagrid.searchEdit({
		idField:'Id',
		page_id:"491330364539666432",
		//initColumnUrl:"/authenticated/grid/column/v2/379254889454895104",
		grid_id:"grid2",
		sqlname:"grid_491330364539666432_excel_data2",
		showCheckbox:false,
		rownumbers:false,
		paging:"yes",
		autoload:true,
		toolbar_id:"searchbar2",
		//initColumnUrl:"/",
		gridParam:{"tableName":"excel_data2","taskId":taskId,"ascOrDesc":"asc","orderBy":"Id","mapId":0},
		initColumnParam:{"taskId":taskId},
		column_formatter:{
			value:function(value,row,index){
				//return value;
				return "<pre style='overflow:hidden'>"+value+"</pre>";
			},
		},
		searchParamOperator:{
		},
	});
	//tab
	$('#searchButton').bind('click',grid1.load);//查询操作绑定事件
	$('#searchButton2').bind('click',grid2.load);//查询操作绑定事件
	$('#saveButton').bind('click', grid1.accept);//保存datagrid绑定事件
	$('#saveButton2').bind('click', grid2.accept);//保存datagrid绑定事件
	//row
	//$('#addButton').bind('click', addRow);//新增行绑定事件
	//$('#editButton').bind('click',grid1.editRow);//编辑按钮绑定事件
	//$('#removeButton').bind('click',grid1.remove);//删除行绑定事件
	$('#btn3').bind('click',grid1.remove);//删除行绑定事件
	$('#btn6').bind('click',grid2.remove);//删除行绑定事件
	$('#importBtn3').bind('click', function () {
		//
		$.post("/excelsearch/importSubTable",{taskId:taskId},function (result){
			if(result.success){
				info("导入成功");
			}else{
				$.messager.alert("提示", "操作失败！<br>"+result.message);
			}
		});
	});//保存datagrid绑定事件

	$('#initBtn4').bind('click', function () {
		//
		$.post("/excelsearch/initMapping",{taskId:taskId},function (result){
			if(result.success){
				info("初始化成功");
			}else{
				$.messager.alert("提示", "操作失败！<br>"+result.message);
			}
		});
	});
	//$('#cancelEditButton').bind('click',grid1.cancelEditing);//取消行编辑绑定事件

	$('#exportData1').bind('click', function(){
		$.post("/compare/exportData1",{taskId:taskId,tableName:"excel_data1"},function (result){
			if(result.success){
				$.easyui.loading();
				startTimeout=0;
				download(result.data);
			}else{
				$.messager.alert("提示", "操作失败！<br>"+result.message);
			}
		});
	});
	$('#exportData2').bind('click', function(){
		$.post("/compare/exportData2",{taskId:taskId,tableName:"excel_data2"},function (result){
			if(result.success){
				$.easyui.loading();
				startTimeout=0;
				download(result.data);
			}else{
				$.messager.alert("提示", "操作失败！<br>"+result.message);
			}
		});
	});

    $('#exportBtn').bind('click',function(){
		$.post("/compare/exportCompareResult",{taskId:taskId},function (result){
			if(result.success){
				$.easyui.loading();
				startTimeout=0;
				download(result.data);
			}else{
				$.messager.alert("提示", "操作失败！<br>"+result.message);
			}
		});
	});//信息变更提示

    //初始化搜索框
    initsearch();
	initWebUploader1();
	initWebUploader2();
	//initWebUploader3();
	//initWebUploader4();
    $('#btn2').bind('click',function(){
    	saveUrl="/authenticated/wx/msg/templateInstance/update";
		gridSelectRow(grid1.grid,function(row){
			editRow=row;
			EditDialogModifyMchId.dialog('setTitle','修改微信模板实例').dialog('open');
		});
	});
    $('#btn1').bind('click', function(){
    	editRow=null;
    	saveUrl="/authenticated/wx/msg/templateInstance/add";
    	EditDialogModifyMchId.dialog('setTitle','新增微信模板实例').dialog('open');
		
	});


    $('#btn5').bind('click',function(){
    	saveUrl2="/authenticated/wx/msg/templateData/update";
		gridSelectRow(grid2.grid,function(row){
			editRow2=row;
			dialog2.dialog('setTitle','修改数据').dialog('open');
		});
	});
    $('#btn4').bind('click', function(){
    	gridSelectRow(grid1.grid,function(row){
    		editRow2=null;
        	saveUrl2="/authenticated/wx/msg/templateData/add";
        	dialog2.dialog('setTitle','新增数据').dialog('open');
		});
    	
		
	});


});
var mytim;

function download(fileItemId){
	//clearTimeout(mytim);
	mytim = setTimeout(function(){
		$.post("/file/downloadQuery/"+fileItemId,null,function (result) {
			if(result.success){
				$.easyui.loaded();
				var form = $("<form>",{
					method:"post",
					action:result.data
				});
				form.appendTo('body').submit().remove();
			}else{
				if(startTimeout<60){
					startTimeout++;
					download(fileItemId)
				}else{
					$.easyui.loaded();
					$.messager.alert("提示", "等待超时");
				}
			}

		});
	},1000);
}
function initWebUploader1(){
	//初始化Web Uploader
	var uploader = WebUploader.create({

		// 选完文件后，是否自动上传。
		auto: true,
		// 文件接收服务端。
		server: '/compare/importData',

		// 选择文件的按钮。可选。
		// 内部根据当前运行是创建，可能是input元素，也可能是flash.
		pick: {
			id: "#importBtn1"
		},
		formData: {
			//excelConfigId:excelConfigId //非固定值不起作用
		},
		// 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
		resize: false,
		accept: {
			title: 'excel',
			extensions: 'xlsx',//没有测试是否起作用
			mimeTypes:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'//这个起作用
		}
	});
	uploader.on('beforeFileQueued', function(file) {
		console.log("beforeFileQueued");
		console.log(file);
		if (file.ext !== 'xlsx') {
			// 文件格式不符合要求，进行处理
			return false;
		}
		uploader.options.formData={taskId:taskId,reportId:report1.id,isAppend:0};
		$.easyui.loading();
	});
	// 当有文件被添加进队列的时候
	uploader.on( 'fileQueued', function( file ) {
		console.log("fileQueued");
		console.log(file);
		if (file.ext !== 'xlsx') {
			// 文件格式不符合要求，进行处理
			uploader.removeFile(file, true);
			return false;
		}

	});


	uploader.on( 'uploadSuccess', function( file,response ) {
		console.log(response);
		if(!response.success){
			$.messager.alert("提示", "导入失败！<br>"+response.message);
		}else{
			info("导入成功");
		}
		//$("#cert_id").val(response.data[0].item_id);
		//$( '#'+file.id ).find('p.state').text('已上传');
	});

	uploader.on( 'uploadError', function( file ) {
		console.log(file);
		//$( '#'+file.id ).find('p.state').text('上传出错');
	});

	uploader.on( 'uploadComplete', function( file ) {
		$.easyui.loaded();
		//$( '#'+file.id ).find('.progress').fadeOut();
		uploader.reset();
		grid1.load();
	});

	uploader.on('uploadBeforeSend', function(object,data,headers) {
		let access_token=localStorage.getItem("access_token");
		headers.Accept="application/json";
		//headers["X-Requested-With"]="XMLHttpRequest";
		headers.Authorization="Local "+access_token;
		//headers.Authorization=BearerType+access_token;
	});
}
function initWebUploader2(){
	//初始化Web Uploader
	var uploader2 = WebUploader.create({

		// 选完文件后，是否自动上传。
		auto: true,
		// 文件接收服务端。
		server: '/compare/importData',

		// 选择文件的按钮。可选。
		// 内部根据当前运行是创建，可能是input元素，也可能是flash.
		pick: {
			id: "#importBtn2"
		},
		formData: {
			//excelConfigId:excelConfigId //非固定值不起作用
		},
		// 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
		resize: false,
		accept: {
			title: 'excel',
			extensions: 'xlsx',//没有测试是否起作用
			mimeTypes:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'//这个起作用
		}
	});
	uploader2.on('beforeFileQueued', function(file) {
		if (file.ext !== 'xlsx') {
			// 文件格式不符合要求，进行处理
			return false;
		}
		uploader2.options.formData={taskId:taskId,reportId:report1.id,isAppend:1};
		$.easyui.loading();
	});
	// 当有文件被添加进队列的时候
	uploader2.on( 'fileQueued', function( file ) {
		if (file.ext !== 'xlsx') {
			// 文件格式不符合要求，进行处理
			uploader2.removeFile(file, true);
			return false;
		}

	});


	uploader2.on( 'uploadSuccess', function( file,response ) {
		console.log(response);
		if(!response.success){
			$.messager.alert("提示", "导入失败！<br>"+response.message);
		}else{
			info("导入成功");
		}
		//$("#cert_id").val(response.data[0].item_id);
		//$( '#'+file.id ).find('p.state').text('已上传');
	});

	uploader2.on( 'uploadError', function( file ) {
		console.log(file);
		//$( '#'+file.id ).find('p.state').text('上传出错');
	});

	uploader2.on( 'uploadComplete', function( file ) {
		$.easyui.loaded();
		//$( '#'+file.id ).find('.progress').fadeOut();
		uploader2.reset();
		grid1.load();
	});

	uploader2.on('uploadBeforeSend', function(object,data,headers) {
		let access_token=localStorage.getItem("access_token");
		headers.Accept="application/json";
		//headers["X-Requested-With"]="XMLHttpRequest";
		headers.Authorization="Local "+access_token;
		//headers.Authorization=BearerType+access_token;
	});
}
function onDeleteRow2(){
	gridConfirm(grid1.grid,"是否确认删除",function (row){
		$.post("/authenticated/wx/msg/templateInstance/delete",{"id":row.id},function(result){
			if(result.success){
				info("删除成功！");
				grid1.load();
			}else{
				$.messager.alert("提示", "删除失败！<br>"+result.message);
			}
		});
	});
}

function save(){
	//if($("#form1").form('enableValidation').form('validate')){
		$.post(saveUrl,$("#form1").serializeObject(),function(result){
			if(result.success){
				info( "保存成功！");
				EditDialogModifyMchId.dialog('close');
				grid1.load();
			}else{
				$.messager.alert("提示", "保存失败！<br>"+result.message);
			}
		});
	//}
}
/**
 * 新增一行
 */
function addRow(){
//ROW
	var newRowData={id:guid()};//初始化行数据,creator_id:sessionuser.id,creator_name:sessionuser.nickname,create_time:moment().format('YYYY-MM-DD')
	grid1.grid.datagrid('append',newRowData);	
	//tab
	parent.addTab("新增","/view/sys/wx/msg_templateform.html");
}
function onUpdate(){
	var selectRows=grid1.grid.datagrid('getSelections');
	if(selectRows.length>0){
		parent.addTab("编辑["+selectRows[0].id+"]","/view/sys/wx/msg_templateform.html?id="+selectRows[0].id);
	}else{
		$.messager.alert("提示", "请选择一条数据！");
	}
}
/**
 * 删除提交
 * @param selectRow
 */
function deleteSubmit(selectRow){
	$.post("/authenticated/ajax/update/grid_wx_msg_template-delete",JSON.stringify({ids:[selectRow.id]}),function(result){
		if(result.status==200){
			$.messager.alert("提示", "删除成功！");
			grid1.load();
		}
	});
}

function onDeleteRow(){
	gridConfirm(grid1.grid,"是否确认删除",deleteRow);
}
function deleteRow(row){
	$.post("/authenticated/xx/delete",JSON.stringify({"id":row.id}),function(result){
		if(result.status==200){
			$.messager.alert("提示", "删除成功！");
			grid1.load();
		}else{
			$.messager.alert("提示", "删除失败！<br>"+result.message);
		}
	});
}


/**
 * 初始化搜索条件
 */
function initsearch(){
	/*$('#search-product_class_id').combotree({
		panelHeight:'auto',
		icons: [{
			iconCls:'icon-clear',
			handler:function(){
				$('#search-product_class_id').combotree('clear');
			}
		}],
	    data:grid1.combotreeDatas.product_class_id
	});
	//初始化combobox例子
	$('#search-status').combobox({
		panelHeight:'auto',
		editable:false,
		valueField:'codeValue',
		textField:'codeName',
		icons: [{
			iconCls:'icon-clear',
			handler:function(e){
				$(e.data.target).combobox('clear');
			}
		}],
	    data:grid1.comboboxDatas.status
	});*/
}
</script>
</body>
</html>